<?php
namespace App\Models;

use App\Support\Wechat\User;

class FavorableUsed extends BaseModel
{
    protected $table = "favorable_used";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }

    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 兑换列表
     *
     */
    public function getDuihuanList($request){
          $where['favorable_id'] = $request['f_id'];
          $where['status'] = 1;
          $fav = new Favorable();
          $fav_where['id'] = $request['f_id'];
          $fav_info = $fav->getFavorableInfo($fav_where);
          $order['id'] = 'desc';
          $rows = $this->getList($where,"*",$order,10);
          if ($rows) {
              $rows = $rows->toArray();
              $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
          }
          $user = new \App\Models\User();
          foreach($rows['data'] as $k => $v){
              $u_where['id'] = $v['user_id'];
              $u_info = $user->getOne($u_where);
              $rows['data'][$k]['name'] = $u_info['wx_name'];
              $rows['data'][$k]['id_no'] = $fav_info['data']['id_no'];
              $rows['data'][$k]['favname'] = $fav_info['data']['title'];
          }
          return $rows;
    }

    /**
     * 删除折扣券
     * @param $request
     * @return array
     */
    public function delFavorable($request){
        $where['id'] = $request['id'];
        $res = $this->del($where);
        if(!$res){
            return ['state'=>0,'折扣券删除失败'];
        }
        return ['state'=>1,'折扣券删除成功'];
    }

    /**
     * 已使用优惠券
     * @return mixed
     */
    public function favorableUsedList()
    {
        $where = [];
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
        }
        $fav = new Favorable();
        foreach($rows['data'] as $key => $v){
             $fav_where['id'] = $v['favorable_id'];
             $fav_info = $fav->getFavorableInfo($fav_where);
             $rows['data'][$key]['title'] = $fav_info['data']['title'];
             $rows['data'][$key]['discount'] = $fav_info['data']['discount'];
             $rows['data'][$key]['type'] = $fav_info['data']['type'];
             $rows['data'][$key]['cash'] = $fav_info['data']['cash'];
             $rows['data'][$key]['end_time'] = $fav_info['data']['end_time'];
        }
        return $rows;
    }
    /**
     * 已使用优惠券(我的)
     * @return mixed
     */
    public function favorableList($request)
    {
        $uid = $request['mid'];
        $where['user_id'] = $uid;
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
        }
        $fav = new Favorable();
        foreach($rows['data'] as $key => $v){
            $fav_where['id'] = $v['favorable_id'];
            $fav_info = $fav->getFavorableInfo($fav_where);
            $rows['data'][$key]['title'] = $fav_info['data']['title'];
            $rows['data'][$key]['discount'] = $fav_info['data']['discount'];
            $rows['data'][$key]['type'] = $fav_info['data']['type'];
            $rows['data'][$key]['cash'] = $fav_info['data']['cash'];
            $rows['data'][$key]['end_time'] = $fav_info['data']['end_time'];
        }
        return $rows;
    }

}